<!--
  角色管理父页面
-->

<template>
  <div class="app-container">
    <el-row :gutter="20" class="header">
      <el-col :span="7">
        <el-input placeholder="请输入角色名..." v-model="queryForm.query" clearable></el-input>
      </el-col>
      <el-button type="primary" :icon="Search" @click="initRoleList">搜索</el-button>

      <el-button type="success" :icon="DocumentAdd" @click="handleDialogValue()">新增</el-button>

      <el-popconfirm title="您确定批量删除这些记录吗？" @confirm="handleDelete(null)">
        <template #reference>
          <el-button type="danger" :disabled="delBtnStatus" :icon="Delete" >批量删除</el-button>
        </template>
      </el-popconfirm>

    </el-row>
    <el-table :data="tableData" stripe style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" />
      <el-table-column prop="name" label="角色名" width="100" align="center"/>
      <el-table-column prop="code" label="权限字符" width="200" align="center"/>
      <el-table-column prop="createTime" label="创建时间" width="200" align="center"/>
      <el-table-column prop="remark" label="备注" />
      <el-table-column prop="action" label="操作" width="400" fixed="right"
                       align="center">
        <template v-slot="scope" >
          <el-button type="primary" :icon="Tools"
                     @click="handleMenuDialogValue(scope.row.id)">分配权限</el-button>

          <el-button v-if="scope.row.code!='admin'" type="primary" :icon="Edit"
                       @click="handleDialogValue(scope.row.id)" />

            <el-popconfirm v-if="scope.row.code!='admin'" title="您确定要删除这条记录吗？"
                           @confirm="handleDelete(scope.row.id)">
              <template #reference>
                <el-button type="danger" :icon="Delete" />
              </template>
            </el-popconfirm>

        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        v-model:currentPage="queryForm.pageNum"
        v-model:page-size="queryForm.pageSize"
        :page-sizes="[10, 20, 30, 40]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />
  </div>
  <Dialog v-model="dialogVisible" :dialogVisible="dialogVisible" :id="id"
          :dialogTitle="dialogTitle" @initRoleList="initRoleList"/>

  <MenuDialog v-model="menuDialogVisible" :menuDialogVisible="menuDialogVisible"
              :id="id" @initRoleList="initRoleList"/>
</template>

<script setup>

import {ref} from "vue";
import requestUtil,{getBaseUrl} from "@/util/request";
import { Search ,Delete,DocumentAdd ,Edit, Tools, RefreshRight} from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import Dialog from "@/views/sys/role/components/dialog.vue";
import MenuDialog from "@/views/sys/role/components/menuDialog.vue";

const total = ref(0)
const tableData = ref([])
const queryForm = ref({
  query: '',
  pageNum: 1,
  pageSize: 10,
})

const initRoleList= async ()=>{
  let res=await requestUtil.post('/sys/role/list',queryForm.value)
  tableData.value = res.data.roleList
  total.value = res.data.total
}

initRoleList();

const handleSizeChange=(pageSize)=>{
  queryForm.value.pageNum=1;
  queryForm.value.pageSize=pageSize;
  initRoleList()
}
const handleCurrentChange=(pageNum)=>{
  queryForm.value.pageNum=pageNum;
  initRoleList()
}

// 初始化对话框中携带的信息
const dialogTitle=ref("")
const id=ref(-1)

// 编辑和新增时对对话框的操作
// 新增的时候：userId 为空
// 编辑的时候：userId 不为空
const handleDialogValue=(userId)=>{
  if(userId){
    id.value=userId;
    dialogTitle.value="角色修改"
  }else{
    id.value=-1;
    dialogTitle.value="角色添加"
  }
  dialogVisible.value=true //打开对话框
}


// 批量删除按钮 这个为true表示置灰
const delBtnStatus=ref(true)

// 定义选中的行，等会要做批量删除
const multipleSelection=ref([])

//一旦有勾选了的行，那么批量删除按钮就不再置灰； 同时给选中的行赋值
const handleSelectionChange=(selection)=>{
  console.log("勾选了")
  console.log(selection)
  multipleSelection.value=selection;
  delBtnStatus.value=selection.length==0;
}

// 处理删除
const handleDelete=async (id)=>{
  var ids = []
  if(id){
    ids.push(id)
  }else{
    multipleSelection.value.forEach(row=>{
      ids.push(row.id)
    })
  }
  const res=await requestUtil.post("sys/role/delete",ids)
  if(res.code== '0'){
    ElMessage({
      type: 'success',
      message: '执行成功!'
    })
    initRoleList();  // 刷新表格数据
  }else{
    ElMessage({
      type: 'error',
      message: res.msg,
    })
  }
}



// 重置密码
const handleResetPassword=async (id)=>{
  const res=await requestUtil.get("sys/user/resetPassword/"+id)
  if(res.code=='0'){
    ElMessage({
      type: 'success',
      message: '执行成功!'
    })
    initRoleList();
  }else{
    ElMessage({
      type: 'error',
      message: res.data.msg,
    })
  }
}


// 修改用户状态
const statusChangeHandle=async (row)=>{
  let res=await
      requestUtil.get("sys/user/updateStatus/"+row.id+"/status/"+row.status);
  if(res.code=='0'){
    ElMessage({
      type: 'success',
      message: '执行成功!'
    })
  }else{
    ElMessage({
      type: 'error',
      message: res.data.msg,
    })
    initRoleList();
  }
}


// ==============操作菜单=================

const menuDialogVisible = ref(false)
// 分配菜单权限
const handleMenuDialogValue=(roleId)=>{
  console.log("roleId="+roleId)
  if (roleId) {
    id.value=roleId;
  }
  menuDialogVisible.value=true  //打开菜单对话框
}


</script>

<style>

.header{
  padding-bottom: 16px;
  box-sizing: border-box;
}
.el-pagination{
  float: right;
  padding: 20px;
  box-sizing: border-box;
}
::v-deep th.el-table__cell{
  word-break: break-word;
  background-color: #f8f8f9 !important;
  color: #515a6e;
  height: 40px;
  font-size: 13px;
}
.el-tag--small {
  margin-left: 5px;
}
</style>